package com.travelshare.business.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.travelshare.common.core.domain.TreeSelect;
import com.travelshare.common.core.domain.entity.TreeDetailReview;
import com.travelshare.common.utils.DateUtils;
import com.travelshare.common.utils.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.travelshare.business.mapper.DetailReviewMapper;
import com.alibaba.fastjson2.JSON;
import com.travelshare.business.domain.DetailReview;
import com.travelshare.business.service.IDetailReviewService;

/**
 * 用户文章评论Service业务层处理
 * 
 * @author travelshare
 * @date 2024-03-05
 */
@Service
public class DetailReviewServiceImpl implements IDetailReviewService 
{
    @Autowired
    private DetailReviewMapper detailReviewMapper;

    /**
     * 查询用户文章评论
     * 
     * @param reviewId 用户文章评论主键
     * @return 用户文章评论
     */
    @Override
    public DetailReview selectDetailReviewByReviewId(Long reviewId)
    {
        return detailReviewMapper.selectDetailReviewByReviewId(reviewId);
    }

    /**
     * 查询用户文章评论列表
     * 
     * @param detailReview 用户文章评论
     * @return 用户文章评论
     */
    @Override
    public List<DetailReview> selectDetailReviewList(DetailReview detailReview)
    {
        return detailReviewMapper.selectDetailReviewList(detailReview);
    }

    /**
     * 新增用户文章评论
     * 
     * @param detailReview 用户文章评论
     * @return 结果
     */
    @Override
    public int insertDetailReview(DetailReview detailReview)
    {
    	DetailReview info = detailReviewMapper.selectDetailReviewByReviewId(detailReview.getParentId());
    	String ancestors = "";
    	if(StringUtils.isNotNull(info)) {
    		detailReview.setReviewType(0);
    		ancestors = info.getAncestors() + "," + detailReview.getParentId();
    	}else {
    		detailReview.setReviewType(1);
    		ancestors = detailReview.getParentId() + "";
    	}
		detailReview.setAncestors(ancestors);
        detailReview.setCreateTime(DateUtils.getNowDate());
        return detailReviewMapper.insertDetailReview(detailReview);
    }

    /**
     * 修改用户文章评论
     * 
     * @param detailReview 用户文章评论
     * @return 结果
     */
    @Override
    public int updateDetailReview(DetailReview detailReview)
    {
        detailReview.setUpdateTime(DateUtils.getNowDate());
        return detailReviewMapper.updateDetailReview(detailReview);
    }

    /**
     * 批量删除用户文章评论
     * 
     * @param reviewIds 需要删除的用户文章评论主键
     * @return 结果
     */
    @Override
    public int deleteDetailReviewByReviewIds(Long[] reviewIds)
    {
        return detailReviewMapper.deleteDetailReviewByReviewIds(reviewIds);
    }

    /**
     * 删除用户文章评论信息
     * 
     * @param reviewId 用户文章评论主键
     * @return 结果
     */
    @Override
    public int deleteDetailReviewByReviewId(Long reviewId)
    {
        return detailReviewMapper.deleteDetailReviewByReviewId(reviewId);
    }

	@Override
	public boolean checkIsFirstReview(Long detailId) {
		int res = detailReviewMapper.checkIsFirstReviewCount(detailId);
		return res == 0;
	}

	@Override
	public List<TreeSelect> selectDetailReviewTreeList(DetailReview detailReview) {
		List<DetailReview> detailReviews = detailReviewMapper.selectDetailReviewList(detailReview);
		String data = JSON.toJSONString(detailReviews);
		List<TreeDetailReview> list = JSON.parseArray(data, TreeDetailReview.class);
        return buildDetailReviewTreeSelect(list);
	}

	@Override
	public List<TreeDetailReview> buildDetailReviewTree(List<TreeDetailReview> detailReviews) {
		List<TreeDetailReview> returnList = new ArrayList<TreeDetailReview>();
        List<Long> tempList = detailReviews.stream().map(TreeDetailReview::getReviewId).collect(Collectors.toList());
        for (TreeDetailReview detailReview : detailReviews)
        {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(detailReview.getParentId()))
            {
                recursionFn(detailReviews, detailReview);
                returnList.add(detailReview);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = detailReviews;
        }
        return returnList;
	}

	@Override
	public List<TreeSelect> buildDetailReviewTreeSelect(List<TreeDetailReview> detailReviews) {
		List<TreeDetailReview> detailReviewTrees = buildDetailReviewTree(detailReviews);
        return detailReviewTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
	}
	
	  /**
     * 递归列表
     */
    private void recursionFn(List<TreeDetailReview> list, TreeDetailReview t)
    {
        // 得到子节点列表
        List<TreeDetailReview> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TreeDetailReview tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<TreeDetailReview> getChildList(List<TreeDetailReview> list, TreeDetailReview t)
    {
        List<TreeDetailReview> tlist = new ArrayList<TreeDetailReview>();
        Iterator<TreeDetailReview> it = list.iterator();
        while (it.hasNext())
        {
        	TreeDetailReview n = (TreeDetailReview) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getReviewId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TreeDetailReview> list, TreeDetailReview t)
    {
        return getChildList(list, t).size() > 0;
    }

	@Override
	public List<TreeSelect> selectDetailReviewChildTreeListByDetailId(Long detailId) {
		List<DetailReview> detailReviews = detailReviewMapper.selectChildrenDeptById(detailId);
		String data = JSON.toJSONString(detailReviews);
		List<TreeDetailReview> list = JSON.parseArray(data, TreeDetailReview.class);
        return buildDetailReviewTreeSelect(list);
	}
}
